package com.wanmait.travel.controller.user;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.travel.dao.CommentDAO;
import com.wanmait.travel.vo.Comment;
import com.wanmait.travel.vo.Userinfo;
@WebServlet("/CommentController")
public class CommentController extends HttpServlet{
	private CommentDAO commentDAO = CommentDAO.getDAO();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
	
		String action = request.getParameter("action");
	
		if(action==null) {
			return;
		}
		switch(action) {
		case "add":
			this.add(request, response);
			break;
		case "listComment":
			this.listComment(request, response);
			break;
		case "deleted":
			this.deleted(request,response);
			break;
		case "anding":
			this.anding(request,response);
			break;
		}
	}
	protected void listComment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String scenicidstr = request.getParameter("scenicid");
		int scenicid = Integer.parseInt(scenicidstr);
		List<Comment> comments = commentDAO.findByScenicId(scenicid);
		request.setAttribute("comments", comments);
		request.getRequestDispatcher("/ajax/comment.jsp").forward(request, response);
	}
	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Userinfo userinfo = (Userinfo)request.getSession().getAttribute("userinfo");
		if(userinfo==null) {
			
			response.getWriter().write("1");
			
		}else{
		
		int userinfoid = userinfo.getId();
		String scenicidstr = request.getParameter("scenicid");
		int scenicid = Integer.parseInt(scenicidstr);
		String comment = request.getParameter("comment");
		
		Comment comments = new Comment();
		comments.setScenicId(scenicid);
		comments.setUserId(userinfoid);
		comments.setComment(comment);
		commentDAO.insert(comments);
		}
	}
	protected void deleted(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idstr=request.getParameter("id");
		int id=Integer.parseInt(idstr);
		commentDAO.deleted(id);

	}
	protected void anding(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String scenicidstr=request.getParameter("scenicid");
		int scenicid = Integer.parseInt(scenicidstr);
		String andingstr = request.getParameter("anding");
		int anding=Integer.parseInt(andingstr);
		commentDAO.insertAnding(scenicid,anding);
	}
	
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	
	
	
}
